

package net.jjjshop.front.controller.user;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.util.alimini.AlipaySystemOauthTokenUtils;
import net.jjjshop.framework.common.api.ApiCode;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.framework.shiro.util.JwtTokenUtil;
import net.jjjshop.front.controller.BaseController;
import net.jjjshop.front.param.AppAliParam;
import net.jjjshop.front.param.AppWxParam;
import net.jjjshop.front.service.user.UserService;
import net.jjjshop.front.vo.user.LoginUserTokenVo;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "user", tags = {"I-用户支付宝"})
@RestController
@RequestMapping("/front/user/userAli")
public class UserAliController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private AlipaySystemOauthTokenUtils alipaySystemOauthTokenUtils;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @OperationLog(name = "login")
    @ApiOperation(value = "login", response = String.class)
    public ApiResult<LoginUserTokenVo> login(@RequestBody AppAliParam appAliParam){
        log.debug("login...");
        LoginUserTokenVo user = userService.loginAli(appAliParam);
        if (user == null) {
            return ApiResult.fail( ApiCode.PARAMETER_EXCEPTION,user);
        }
        // 设置token响应头
        response.setHeader(JwtTokenUtil.getTokenName(""), user.getToken());
        return ApiResult.ok(user, "登录成功");
    }


    @ApiImplicitParams({
            //参数效验
            @ApiImplicitParam(name="code",value="授权码/刷新令牌",required=true,paramType="form"),
            @ApiImplicitParam(name="grantType",value="authorization_code:用授权码来换取授权令牌 ,refresh_token:用刷新令牌来换取一个新的授权令牌",required=true,paramType="form")
    })
    @RequestMapping(value = "/getSession", method = RequestMethod.POST)
    @OperationLog(name = "getSession")
    @ApiOperation(value = "getSession", response = String.class)
    public ApiResult<String> getSessionKey(String code,String grantType){
        String sessionKey = userService.getSessionKeyAli(code,grantType);
        return ApiResult.ok(sessionKey, "");
    }

    /**
     * 解密手机号,绑定手机号
     * @return
     */
    @ApiImplicitParams({
            //参数效验
            @ApiImplicitParam(name="sign",value="手机号授权加密数据",required=true),
            @ApiImplicitParam(name="encryptedData",value="手机号授权加密数据",required=true)
    })
    @RequestMapping(value = "/bindPhoneNumber", method = RequestMethod.POST)
    @OperationLog(name = "bindPhoneNumber")
    @ApiOperation(value = "解密手机号,绑定手机号", response = String.class)
    public ApiResult<String> bindPhoneNumber(String sign,String encryptedData){

        String phoneNumber = alipaySystemOauthTokenUtils.getPhoneNumber(sign,encryptedData);
        if(StringUtils.isNotBlank(phoneNumber)){
            userService.updateInfo(getUser(true).getUserId(), "phone", phoneNumber);
        }
        return ApiResult.ok("");
    }


    @RequestMapping(value = "/bindMobile", method = RequestMethod.POST)
    @OperationLog(name = "bindMobile")
    @ApiOperation(value = "bindMobile", response = String.class)
    public ApiResult<String> bindMobile(String sessionKey, String encryptedData, String iv){
        User user = this.getUser(true);
        String mobile = userService.bindMobileByWx(user.getUserId(), sessionKey, encryptedData, iv);
        if(!"".equals(mobile)){
            return ApiResult.ok(mobile, "");
        }
        return ApiResult.fail("绑定失败，请重试");
    }
}
